<<<<<<< HEAD
# 通过人类相互作用组将多酚靶点与致病蛋白联系起来
import pandas as pd
from collections import defaultdict
import networkx as nx

# 读入多酚名称与靶点蛋白
polyphenol = pd.read_csv('./data/30pro/drug.csv')
chemical2genes = defaultdict(list)
for i in polyphenol.index:
    chemical2genes[polyphenol['chemical'].loc[i]].append(
        int(polyphenol['entrez_id'].loc[i]))
# 'chemical', 'entrez_id' 为文件表头


# 读入疾病分类与节点蛋白
dg = pd.read_csv('./data/30pro/dis.csv')
disease2genes = defaultdict(list)
for i in dg.index:
    disease2genes[dg['disease'].loc[i]].append(
        int(dg['entrez_id'].loc[i]))

# 读入人类相互作用组，并构成网络
dt = pd.read_csv('./data/30pro/Interactome2018.csv')
edges = zip(dt['EntrezA'], dt['EntrezB'])

G = nx.Graph()
G.add_edges_from(edges)
# 只考虑相互作用组中最大连通分量(LCC)
g = (G.subgraph(c).copy() for c in nx.connected_components(G))
g = list(g)[0]

    
# 与多酚相关的节点
targets = set()
for i in chemical2genes.values():
    targets.update(i)
    
target_nodes = targets & set(G.nodes())  #  & 并集

# 与疾病蛋白相关的节点
proteins = set()
for i in disease2genes.values():
    proteins.update(i)

protein_nodes = proteins & set(G.nodes())

# 为网络添加标签，标签位于PolyphenolProteinInteractions.csv中
symbol_map = dict(zip((polyphenol['entrez_id']).astype('int'),
                      polyphenol['symbol']))
# g = nx.relabel_nodes(g, symbol_map)

# 取出子网络
sgt = nx.subgraph(g, target_nodes)

# chemical2genes keys
chemical = 'pro'
if chemical in chemical2genes.keys():
    chemical_nodes = set(chemical2genes[chemical])    
    cg = nx.subgraph(sgt, chemical_nodes)
    cg = nx.relabel_nodes(cg, symbol_map)
    nx.write_gexf(cg, './save/{}.gexf'.format(chemical))
else:
    print('{} is not in chemical2genes'.format(chemical))
nx.write_gexf(sgt, './save/targets.gexf')

sgp = nx.subgraph(g, protein_nodes)
nx.write_gexf(sgp, './save/proteins.gexf')
=======
# 通过人类相互作用组将多酚靶点与致病蛋白联系起来
import pandas as pd
from collections import defaultdict
import networkx as nx
#---------------------------------------------------读入数据----------------------------------------
# 读入多酚名称与靶点蛋白
polyphenol = pd.read_csv('./data/PolyphenolProteinInteractions.csv')
chemical2genes = defaultdict(list)
for i in polyphenol.index:
    chemical2genes[polyphenol['chemical'].loc[i]].append(
        int(polyphenol['entrez_id'].loc[i]))
# 'chemical', 'entrez_id' 为文件表头


# 读入疾病分类与节点蛋白
dg = pd.read_csv('./data/GenesDisease.csv')
disease2genes = defaultdict(list)
for i in dg.index:
    disease2genes[dg['disease'].loc[i]].append(
        int(dg['entrez_id'].loc[i]))

# 读入人类相互作用组，并构成网络
dt = pd.read_csv('./data/HumanInteractome_v2017.csv')
edges = zip(dt['EntrezA'], dt['EntrezB'])
#---------------------------------------------------处理----------------------------------------

G = nx.Graph()
G.add_edges_from(edges)
# 只考虑相互作用组中最大连通分量(LCC)
g = (G.subgraph(c).copy() for c in nx.connected_components(G))
g = list(g)[0]

    
# 与多酚相关的节点
targets = set()
for i in chemical2genes.values():
    targets.update(i)
    
target_nodes = targets & set(G.nodes())  #  & 交集

# 与疾病蛋白相关的节点
proteins = set()
for i in disease2genes.values():
    proteins.update(i)

protein_nodes = proteins & set(G.nodes())

# 为网络添加标签，标签位于PolyphenolProteinInteractions.csv中
symbol_map = dict(zip((polyphenol['entrez_id']).astype('int'),
                      polyphenol['symbol']))
# g = nx.relabel_nodes(g, symbol_map)
#---------------------------------------------------输出----------------------------------------

# 取出子网络
sgt = nx.subgraph(g, target_nodes)

# chemical2genes keys
# chemical = '(-)-epicatechin 3-o-gallate'
chemical = 'nervous system diseases'

if chemical in chemical2genes.keys():
    chemical_nodes = set(chemical2genes[chemical])    
    cg = nx.subgraph(sgt, chemical_nodes)
    cg = nx.relabel_nodes(cg, symbol_map)
    nx.write_gexf(cg, '{}.gexf'.format(chemical))
else:
    print('{} is not in chemical2genes'.format(chemical))
nx.write_gexf(sgt, 'targets.gexf')

sgp = nx.subgraph(g, protein_nodes)
nx.write_gexf(sgp, 'proteins.gexf')
>>>>>>> 3de9897e51d6cc3ea99d1fd8c01fc43ca09c30af
